package Managers;

import java.awt.Color;
import java.util.LinkedList;
import java.util.List;

/*
 * Category Manager.
 * 
 * Stores the default categories, and supports addition of custom categories.
 * 
 * Since the CategoryManager Implements the singleton pattern, all methods which
 * can be are static, because there should only ever been one category manager.
 */
public class CategoryManager implements ICatObservable {

	// Private list to store categories.
	private List<Category> categories;
	private List<ICatObserver> liCatsObs;
	// Singleton pattern - instance.
	private static CategoryManager instance;

	// Singleton pattern - private constructor.
	private CategoryManager() {
		categories = new LinkedList<Category>();
		liCatsObs = new LinkedList<ICatObserver>();
		
		categories.add(new Category("University", Color.RED));
		categories.add(new Category("Social", Color.BLUE));
		categories.add(new Category("Job", Color.YELLOW));
		categories.add(new Category("None", Color.WHITE));
	}

	// Singleton pattern - expose instance.
	public static CategoryManager getInstance() {
		if (instance == null)
			instance = new CategoryManager();
		return instance;
	}

	public List<Category> getCategories() {
		return categories;
	}
	
	// Add a new custom category.
	public static void addCategory(String name, Color colour)
	{
		Category x = new Category(name, colour);
		getInstance().getCategories().add(x);
		getInstance().notifyObservers(x);
		
	}

	// Return list of categories.
	public static Category[] getList()
	{
		return getInstance().getCategories().toArray(new Category[] {});
	}

	public static Category getCategory(String s)
	{
		for(Category c : getInstance().getCategories())
		{
			if(c.getName().equalsIgnoreCase(s))
			{
				return c;
			}
		}
		
		return null;
	}

	
	@Override
	public void addObserver(ICatObserver io) {
		liCatsObs.add(io);
	}

	@Override
	public void notifyObservers(Category c) {
		for(ICatObserver ico : liCatsObs)
		{
			ico.Update(c);
		}
		
	}
	
}
